package process2

import (
	"chatroom/common/message"
	"chatroom/server/utils"
	"encoding/json"
	"fmt"
	"net"
)

type UserProcess struct {
	Conn net.Conn
}

func (this *UserProcess) ServerProcessLogin(mes *message.Message)(err error)  {
	var loginMes message.LoginMes			//用于用户登录传进来的参数
	var loginResMes message.LoginResMes		//用于封装返回用户登录结果
	err = json.Unmarshal([]byte(mes.Data), &loginMes)
	if err != nil {
		fmt.Println("json.unmarshal fail error = ",err)
		return
	}

	if loginMes.UserId == 100 && loginMes.UserPwd == "123456" {
		loginResMes.Code = 200
	}else {
		loginResMes.Code = 500
		loginResMes.Error = "用户名或密码错误"
	}

	var resMes message.Message
	data, err := json.Marshal(loginResMes)
	if err != nil {
		fmt.Println("json.Marshal(loginResMes) fail",err)
		return
	}

	resMes.Data = string(data)
	data, err = json.Marshal(resMes)
	if err != nil {
		fmt.Println("json.Marshal(resMes) fail",err)
		return
	}

	tf := &utils.Transfer{
		Conn :this.Conn,
	}

	err = tf.WritePkg(data)
	return
}